package com.xintonghua.bussiness.ui.user;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMMin;
import com.xintonghua.base.widget.ConfirmDialog;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.ShareBean;
import com.xintonghua.bussiness.ui.H5Activitys;
import com.xintonghua.bussiness.ui.view.PopupShare;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.SaveImageUtils;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：wanxianyi
 * 时间：2018/3/28.
 * 包名：com.xintonghua.bussiness.ui.user
 */

public class ShareActivity extends BaseActivity implements PopupShare.MyClickListener {
    @BindView(R.id.ima_bg)
    ImageView imaBg;
    @BindView(R.id.p_num)
    TextView pNum;
    @BindView(R.id.j_money)
    TextView jMoney;
    @BindView(R.id.iv_share_qr)
    ImageView ivShareQr;
    @BindView(R.id.tv_share)
    TextView tvShare;
    @BindView(R.id.iv_share)
    ImageView ivShare;
    private PopupShare popupShare;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_share);
        ButterKnife.bind(this);
        initUI();

    }

    @Override
    public void initUI() {
        super.initUI();
        //popupShare = new PopupShare(this, true);
        //popupShare.setMyClickListener(this);
        httpCent.getMyShared(this, MyState.requestOne);
        httpCent.getRrCode(this, MyState.requestTwo);
    }

    @OnClick({R.id.iv_back, R.id.tv_share, R.id.iv_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_share:
                shareMini();
                // popupShare.showPopupWindow();
                break;
            case R.id.iv_share:
                Bundle bundle = new Bundle();
                bundle.putString(BuildConfig.FLAVOR, "http://hy.nxshare.xyz/info.html");
                openActivity(H5Activitys.class, bundle);
                break;
        }
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                ShareBean bean = JSONObject.parseObject((String) data, ShareBean.class);
                pNum.setText(bean.getNum() + "人");
                jMoney.setText(bean.getTotalScore());
                break;
            case MyState.requestTwo:
                final String name = (String) data;
                Glide.with(this)
                        .load("https://e426.cn/hongyi/upload/" + name)
                        .asBitmap()
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                                ivShareQr.setImageBitmap(bitmap);
                                httpCent.deleteRrCode(name, ShareActivity.this, MyState.requestThree);
                            }

                            @Override
                            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                                super.onLoadFailed(e, errorDrawable);
                            }
                        });
                ivShareQr.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        ConfirmDialog.showDialog(ShareActivity.this, "提示", "您确认保存图片到本地?", "取消", "确定", null,
                                new ConfirmDialog.OnRightListener() {
                                    @Override
                                    public void onClick(ConfirmDialog dialog) {
                                        ivShareQr.setDrawingCacheEnabled(true);
                                        Bitmap imageBitmap = ivShareQr.getDrawingCache();
                                        if (imageBitmap != null) {
                                            new SaveImageUtils(ShareActivity.this, ivShareQr).execute(imageBitmap);
                                        }
                                    }
                                });
                        return true;
                    }
                });
                break;
        }
    }

    private void shareMini() {
        IWXAPI iwxapi = WXAPIFactory.createWXAPI(this, "wxaef20a1fd159db53", false);
        if (!iwxapi.isWXAppInstalled()) {
            mToast("您还没有安装微信哦,请先安装微信哦");
            return;
        }
        UMMin umMin = new UMMin("https://www.pgyer.com/lGkc");
        UMImage umImage = new UMImage(this, R.drawable.icon);
        umMin.setThumb(umImage);
        umMin.setTitle(getString(R.string.app_name));
        umMin.setDescription("科技美容、健康养生、减脂塑形；咨询、预约服务，您最贴心的美容服务平台");
        umMin.setPath("pages/register/register");
        umMin.setUserName("gh_ddd085222554");
        new ShareAction(ShareActivity.this)
                .withMedia(umMin)
                .setPlatform(SHARE_MEDIA.WEIXIN)
                .setCallback(shareListener)
                .share();
    }


    @Override
    public void share(SHARE_MEDIA type) {

    }


}
